//
//  WaySummaryViewController.m
//  CarCare
//
//  Created by ileo on 15/4/9.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "WaySummaryViewController.h"
#import "SummaryMgr.h"
#import "SummaryCell.h"
#import "BaseWebViewController.h"

typedef NS_ENUM(NSInteger , View_Type) {
    View_Type_Label = 10,
    View_Type_Button = 20,
};

#define TOP (44 * __MAIN_RATIO_375)

@interface WaySummaryViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) SummaryMgr *summaryMgr;

@property (nonatomic, copy) NSArray *summaryTypes;

//@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic , readonly) NSArray * headTitleArr;

@property (nonatomic , strong) UIView * headView;

@property (nonatomic , strong) UIView * headLineView;

@end

@implementation WaySummaryViewController

-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    
    [CommentHelper useFunction:Use_TripStatistics];
    
    [UmengMgr event:ICZ_Trip_Stats];
    
    __weak __typeof(self) wself = self;
    
    self.title = @"趋势统计";
    self.view.backgroundColor = [Theme redColor];
    
    self.summaryTypes = @[@(SummaryData_Mile), @(SummaryData_OilPrice), @(SummaryData_Mark), @(SummaryData_AvgOil)];
    self.summaryMgr = [[SummaryMgr alloc] init];
    
    [self.summaryMgr needUpdate:^{
        [wself.tableView reloadData];
    }];
    
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.top = TOP;
    self.tableView.height -= (TOP + __MAIN_TABBAR_HEIGHT);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self createHeadView];
//    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"周",@"月",@"年"]];
//    self.segmentedControl.frame = CGRectMake(0, 0, 280 *__MAIN_RATIO_320 , 29);
//    self.segmentedControl.center = CGPointMake(__MAIN_WIDTH / 2, TOP / 2);
//    [self.segmentedControl addTarget:self action:@selector(segmentedChange:) forControlEvents:UIControlEventValueChanged];
//    self.segmentedControl.tintColor = [UIColor whiteColor];
//    self.segmentedControl.selectedSegmentIndex = 0;
//    [self.view addSubview:self.segmentedControl];
    
    [[[UIView viewWithFrame:CGRectMake(0, TOP, __MAIN_WIDTH, 0.5)] resetBackgroundColor:COLOR_HEXSTRING(@"#e2d9c9")] setupOnView:self.view];
}
- (NSArray *)headTitleArr
{
    return @[@"每周",@"每月",@"每年"];
}
- (void)createHeadView
{
    self.headView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, TOP)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self.view];
    CGFloat headLabelW = __MAIN_WIDTH / self.headTitleArr.count;
    for (int index = 0; index < self.headTitleArr.count; index ++) {
        UILabel * headLabel = [Factory labelWithFrame:CGRectMake(index * headLabelW, 0, headLabelW, TOP) font:FONT_DEFAULT_Light(13) text:self.headTitleArr[index] textColor:[Theme wordColor] onView:self.headView textAlignment:NSTextAlignmentCenter];
        headLabel.tag = View_Type_Label + index;
        headLabel.userInteractionEnabled = YES;
        
        if(index == 0)self.headLineView = [[[UIView viewWithFrame:CGRectMake(0 , TOP - 2, headLabelW, 2)] resetBackgroundColor:[Theme mainNavColor]] setupOnView:self.headView];
        
        UIButton * headButton = [[UIButton buttonEmptyWithFrame:headLabel.bounds click:nil] setupOnView:headLabel];
        headButton.tag = View_Type_Button + index;
        [headButton addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        if(index != 0)[[[UIView viewWithFrame:CGRectMake(headLabel.left, 12 * __MAIN_RATIO_375, 0.5, TOP - 12 * __MAIN_RATIO_375 * 2)] resetBackgroundColor:COLOR_HEXSTRING(@"#E6E6E6")] setupOnView:self.headView];

    }
}
- (void)headButtonClick:(UIButton *)sender
{
    [self reloadHeadView];
    UILabel * headLabel = (UILabel *)[self.headView viewWithTag:View_Type_Label + (sender.tag - View_Type_Button)];
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 animations:^{
        [headLabel setTextColor:[Theme mainNavColor]];
        [wself.headLineView setLeft:headLabel.left];
    }];
    
    switch (sender.tag - View_Type_Button) {
        case 0:
            self.summaryMgr.type = Summary_Week;
            break;
        case 1:
            self.summaryMgr.type = Summary_Month;
            break;
        case 2:
            self.summaryMgr.type = Summary_Year;
            break;
        default:
            break;
    }
}
- (void)reloadHeadView
{
    for (int index = 0; index < self.headTitleArr.count;  index ++) {
        UILabel * headLabel = (UILabel *)[self.headView viewWithTag:View_Type_Label + index];
        [headLabel setTextColor:[Theme wordColor]];
    }
}
-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    [self.summaryMgr clearDatas];
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.summaryTypes.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SummaryCell cellHeight];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    SummaryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[SummaryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.model = [self.summaryMgr modelWithType:[self.summaryTypes[indexPath.row] integerValue]];
    
    return cell;
}


@end
